<?php
declare (strict_types = 1);

namespace app\service\logic\backend\system;

use app\constant\LogicConstant;
use app\model\communal\AdminModel;
use Exception;
use think\Collection;
use app\model\communal\MenuModel;
use think\db\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

final class MenuService
{
    /**
     * 根据条件获取菜单列表
     *
     * @param AdminModel $admin
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getListByPermissions(AdminModel $admin)
    {
        // $frontEnd = invoke(PermissionsService::class)->getFrontEndByAuth(); // debug 阶段注释 权限相关
        $list = MenuModel::field(MenuModel::$column)
            // ->whereIn('front_end', $frontEnd) // 注释 调试
            ->order('id asc')
            ->select()
            ->toArray();
        return $this->_getLayoutTree($list);
    }

    /**
     * 获取一组菜单最后子节点path
     *
     * @param $memu
     * @return mixed
     */
    public function getLastPathByMenu($menu)
    {
        $path = $menu['path'];
        if (!empty($menu['children'])) {
            $path = $this->getLastPathByMenu($menu['children'][0]);
        }

        return $path;
    }

    /**
     * 获取菜单树状
     *
     * @param $list
     * @param int $pid
     * @return array
     */
    private function _getLayoutTree($list, $pid = 0)
    {
        $result = [];
        foreach ($list as $item) {
            if ($item['pid'] == $pid) {
                $item['children'] = $this->_getLayoutTree($list, $item['id']);
                $result[] = $item;
            }
        }
        return $result;
    }

    /**
     * 初始化菜单
     *
     * @return Collection
     * @throws Exception
     */
    public function initData()
    {
        // id 即 菜单类型 不可更改
        // fab fa-windows 图标存在显示bug 暂去掉
        $init = [
            /*
             * 工作台/首页
             */
            // ['id' => '1', 'pid' => '0', 'title' => '工作台', 'icon' => 'fa fa-bar-chart', 'front_end' => 'workbench', 'path' => (string)url('/b/home/index'), 'belong' => LogicConstant::AUTH_TYPE_ONE],

            /*
             * 财务管理
             */
            ['id' => '30000', 'pid' => '0', 'title' => '财务管理', 'icon' => 'fa fa-jpy', 'front_end' => 'finance', 'path' => 'finance', 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '31000', 'pid' => '30000', 'title' => '专家余额提现', 'front_end' => 'finance_balance', 'path' => (string)url('/b/fb/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '32000', 'pid' => '30000', 'title' => '元心币提现', 'front_end' => 'finance_coin', 'path' => (string)url('/b/fc/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '33000', 'pid' => '30000', 'title' => '退款订单', 'front_end' => 'finance_refund', 'path' => (string)url('/b/oc/refund'), 'belong' => LogicConstant::AUTH_TYPE_ONE],

            /*
             * 订单管理
             */
            ['id' => '50000', 'pid' => '0', 'title' => '订单管理', 'icon' => 'fa fa-bars', 'front_end' => 'order', 'path' => 'order', 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '50001', 'pid' => '50000', 'title' => '心理测试', 'front_end' => 'order_multi', 'path' => (string)url('/b/om/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '50201', 'pid' => '50000', 'title' => '量表解读', 'front_end' => 'order_consult_1', 'path' => (string)url('/b/oc/view1'), 'belong' => LogicConstant::AUTH_TYPE_ONE],

            /*
             * 客服管理
             */
            ['id' => '68000', 'pid' => '0', 'title' => '客服管理', 'icon' => 'fa fa-users', 'front_end' => 'customer', 'path' => 'customer', 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '68001', 'pid' => '68000', 'title' => '客服列表', 'front_end' => 'customer_list', 'path' => (string)url('/b/customer/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],

            /*
             * 专家管理
             */
            ['id' => '69000', 'pid' => '0', 'title' => '专家管理', 'icon' => 'fa fa-thumbs-up', 'front_end' => 'expert', 'path' => 'expert', 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '69001', 'pid' => '69000', 'title' => '专家列表', 'front_end' => 'expert_list', 'path' => (string)url('/b/expert/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],


            /*
             * 量表管理
             */
            ['id' => '70000', 'pid' => '0', 'title' => '量表管理', 'icon' => 'fa fa-file-text-o', 'front_end' => 'scale', 'path' => 'scale', 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '70001', 'pid' => '70000', 'title' => '量表分类', 'front_end' => 'scale_category', 'path' => (string)url('/b/sc/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '70100', 'pid' => '70000', 'title' => '多元智能题型', 'front_end' => 'scale_multi_element', 'path' => (string)url('/b/multi/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],

            /*
             * 课程管理
             */
            ['id' => '80000', 'pid' => '0', 'title' => '课程管理', 'icon' => 'fa fa-file-video-o', 'front_end' => 'course', 'path' => 'course', 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '81000', 'pid' => '80000', 'title' => '课程分类', 'front_end' => 'course_category', 'path' => (string)url('/b/courseC/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '82000', 'pid' => '80000', 'title' => '课程内容', 'front_end' => 'course_content', 'path' => (string)url('/b/course/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '83000', 'pid' => '80000', 'title' => '视频分类', 'front_end' => 'course_video_category', 'path' => (string)url('/b/vc/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '83001', 'pid' => '80000', 'title' => '视频内容', 'front_end' => 'course_video', 'path' => (string)url('/b/video/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],

            /*
             * 维护管理
             */
            ['id' => '90000', 'pid' => '0', 'title' => '维护管理', 'icon' => 'fa fa-wrench', 'front_end' => 'maintain', 'path' => 'maintain', 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '90001', 'pid' => '90000', 'title' => '广告位维护', 'front_end' => 'maintain_banner', 'path' => (string)url('/b/banner/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '90100', 'pid' => '90000', 'title' => '公告维护', 'front_end' => 'maintain_placard', 'path' => (string)url('/b/placard/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '90200', 'pid' => '90000', 'title' => '咨询师简介', 'front_end' => 'maintain_counselor', 'path' => (string)url('/b/counselor/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '90300', 'pid' => '90000', 'title' => '信息维护', 'front_end' => 'maintain_info', 'path' => (string)url('/b/info/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],

            /*
             * 系统管理
             */
            ['id' => '100000', 'pid' => '0', 'title' => '系统管理', 'icon' => 'fa fa-cog', 'front_end' => 'system', 'path' => 'system', 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '100001', 'pid' => '100000', 'title' => '角色权限', 'front_end' => 'system_role', 'path' => (string)url('/b/role/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
                ['id' => '100100', 'pid' => '100000', 'title' => '组织架构', 'front_end' => 'system_admin', 'path' => (string)url('/b/admin/view'), 'belong' => LogicConstant::AUTH_TYPE_ONE],
        ];

        $exitIds = MenuModel::where(1,1)->column('id');
        $inserts = [];
        foreach ($init as $item) {
            if (in_array($item['id'], $exitIds)) {
                continue ;
            }

            $inserts[] = $item;
        }
        if (!empty($inserts)) {
            return (new MenuModel)->saveAll($inserts, false);
        } else {
            return new Collection();
        }
    }
}
